<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="工程名称" prop="pName">
        <el-input
          v-model="queryParams.pName"
          placeholder="请输入工程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报人" prop="pApplicantName">
        <el-input
          v-model="queryParams.pApplicantName"
          placeholder="请输入工程申报人名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="pHeader">
        <el-input
          v-model="queryParams.pHeader"
          placeholder="请输入工程负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <br>
      <el-form-item label="工程性质" prop="pNatureId">
        <el-select v-model="queryParams.pNatureId" placeholder="请选择工程性质" clearable size="small">
          <el-option
            v-for="dict in pNatureIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工程类别" prop="pCategoryId">
        <el-select v-model="queryParams.pCategoryId" placeholder="请选择工程类别" clearable size="small">
          <el-option
            v-for="dict in pCategoryIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工程等级" prop="pLevelId">
        <el-select v-model="queryParams.pLevelId" placeholder="请选择工程等级" clearable size="small">
          <el-option
            v-for="dict in pLevelIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['declaration:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['declaration:project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['declaration:project:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['declaration:project:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工程id" align="center" prop="pId" />
      <el-table-column label="工程名称" align="center" prop="pName" />
      <el-table-column label="工程申报人名字" align="center" prop="pApplicantName" />
      <el-table-column label="工程负责人" align="center" prop="pHeader" />
      <el-table-column label="工程性质" align="center" prop="pNatureId" :formatter="pNatureIdFormat" />
      <el-table-column label="工程类别" align="center" prop="pCategoryId" :formatter="pCategoryIdFormat" />
      <el-table-column label="工程等级" align="center" prop="pLevelId" :formatter="pLevelIdFormat" />
      <el-table-column label="工程经费预算" align="center" prop="pFund" />
      <el-table-column label="工程进度" align="center" prop="pProgressId" :formatter="pProgressIdFormat" />
      <el-table-column label="工程申报时间" align="center" prop="pApplyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pApplyTime) }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="工程链接" align="center" prop="pLink" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['declaration:project:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRecord(scope.row)"
            v-hasPermi="['declaration:record:list']"
          >查看记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFile(scope.row)"
            v-hasPermi="['declaration:files:list']"
          >查看文件</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['declaration:project:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['declaration:project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 工程审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核意见" prop="pUnitOpinion"> <!--edit++-->
          <el-input v-model="form.pUnitOpinion" :autosize="{ minRows: 2, maxRows: 5}" type="textarea" placeholder="请输入审核意见" /> <!--edit++-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">审核通过</el-button>
        <el-button type="danger" @click="cancel">不通过</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject, exportProject } from "@/api/declaration/project";

export default {
  //inject:['reload'],
  name: "Audit_Project",
  data() {
    return {
      opinion:"",
      nowTime: new Date(), // 当前时间
      user: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 工程认证申报数据表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 工程性质id字典
      pNatureIdOptions: [],
      // 工程类别id字典
      pCategoryIdOptions: [],
      // 工程等级id字典
      pLevelIdOptions: [],
      // 工程成果形式id字典
      pResultFormIdOptions: [],
      // 工程进度id字典
      pProgressIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pApplicantName: undefined,
        pHeader: undefined,
        pNatureId: undefined,
        pCategoryId: undefined,
        pLevelId: undefined,
        pName: undefined,
        pProgressId: undefined,
        pApplicantId:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  filters: {
        formatDate(nowTime) {
          var moment = require("moment");
          return moment(nowTime).format("YYYY-MM-DD HH:mm:ss");
        }
  }, 
  created() {
    this.getList();
    this.getDicts("p_nature_id").then(response => {
      this.pNatureIdOptions = response.data;
    });
    this.getDicts("p_category_id").then(response => {
      this.pCategoryIdOptions = response.data;
    });
    this.getDicts("p_level_id").then(response => {
      this.pLevelIdOptions = response.data;
    });
    this.getDicts("p_result_form_id").then(response => {
      this.pResultFormIdOptions = response.data;
    });
    this.getDicts("p_progress_id").then(response => {
      this.pProgressIdOptions = response.data;
    });
  },
  methods: {

    /** 查询工程认证申报数据列表 */
    getList() {
      this.loading = true;
      
      this.queryParams.pProgressId='1';   //审核 edit++
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 工程性质id字典翻译
    pNatureIdFormat(row, column) {
      return this.selectDictLabel(this.pNatureIdOptions, row.pNatureId);
    },
    // 工程类别id字典翻译
    pCategoryIdFormat(row, column) {
      return this.selectDictLabel(this.pCategoryIdOptions, row.pCategoryId);
    },
    // 工程等级id字典翻译
    pLevelIdFormat(row, column) {
      return this.selectDictLabel(this.pLevelIdOptions, row.pLevelId);
    },
    // 工程成果形式id字典翻译
    pResultFormIdFormat(row, column) {
      return this.selectDictLabel(this.pResultFormIdOptions, row.pResultFormId);
    },
    // 工程进度id字典翻译
    pProgressIdFormat(row, column) {
      return this.selectDictLabel(this.pProgressIdOptions, row.pProgressId);
    },
    // 取消按钮
    cancel() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pId != undefined) {
            this.form.pProgressId="0";  
            this.form.pAuditTime=this.nowTime;    //edit++
            this.form.prName="审核不通过";
            this.form.prContent=this.form.pUnitOpinion;
            updateProject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } 
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        pId: undefined,
        pApplicantId: undefined,
        pApplicantName: undefined,
        pApplyDeptId: undefined,
        pHeader: undefined,
        pHeaderEmail: undefined,
        pHeaderPhone: undefined,
        pNatureId: undefined,
        pCategoryId: undefined,
        pLevelId: undefined,
        pName: undefined,
        pContent: undefined,
        pBenefitAnalysis: undefined,
        pResultFormId: undefined,
        pGuideTeachers: undefined,
        pTeam: undefined,
        pFund: undefined,
        pProgressId: undefined,
        pApplyTime: undefined,
        pAuditTime: undefined,
        pUnitOpinion: undefined,
        pApprovalTime: undefined,
        pInstituteOpinion: undefined,
        pStartTime: undefined,
        pCollegeOpinionStart: undefined,
        pEndTime: undefined,
        pCollegeOpinionEnd: undefined,
        pLink: undefined,
        pAchievement: undefined
      };
      this.resetForm("form");
    },
    /** 查询申报记录操作 */
    handleRecord(row){
      const pId = row.pId;
      this.$router.push({ path: "/declaration/record",query:{pId:pId} });
    },
     /** 查询文件列表操作 */
    handleFile(row){
      const fSourceId = row.pId;
      this.$router.push({ path: "/declaration/files",query:{fSourceId:fSourceId} });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: "/declaration/project/update_project" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pId = row.pId || this.ids
      this.$router.push({ path: "/declaration/project/update_project",query:{pId:pId}});
      /*
      getProject(pId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工程认证申报数据";
      });
      */
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pId != undefined) {
            this.form.pAuditTime=this.nowTime;  //edit++
            this.form.pProgressId='2';            //edit++
            this.form.prName="审核通过";
            this.form.prContent=this.form.pUnitOpinion;
            updateProject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } 
        }
      });
    },
    /** 审核操作 */
    handleAudit(row){
      this.reset();
      const pId = row.pId || this.ids
      getProject(pId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "工程审核";    //edit++
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pIds = row.pId || this.ids;
      this.$confirm('是否确认删除工程认证申报数据编号为"' + pIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProject(pIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工程认证申报数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProject(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>